package yns.springboot.guide.demo.data.mongodb.template;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.MongoDatabaseFactory;

import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoDatabase;

import yns.springboot.guide.demo.configure.data.mongodb.MongoClientConfigure;

@Configuration
public class MongodbTemplateFactory {

//	If you do not use Spring Data MongoDB, you can inject a MongoClient bean instead of using MongoDatabaseFactory. 
//	If you want to take complete control of establishing the MongoDB connection, 
//	you can also declare your own MongoDatabaseFactory or MongoClient bean.
	
	
	@Autowired
	private MongoClientConfigure mongoClientConfigure;

	@Bean
	public MongoClient mongoClientFactory() {
		return MongoClients.create(mongoClientConfigure.getUri());
	}
	
	@Bean
	public MongoDatabase mongoDatabaseSpringFactory(MongoDatabaseFactory mongoDatabaseFactory) {
		return mongoDatabaseFactory.getMongoDatabase(mongoClientConfigure.getDatabase());
	}

}
